//
//  MBDMessageViewModel.swift
//  MasterBoy
//
//  Created by kyson on 2023/11/7.
//

import Foundation
import YYText
import SwiftyJSON

class MBDMessageCellModel : MBDCommonCellModel {
    
    var titlelayout : YYTextLayout!
    var subtitlelayout : YYTextLayout!
    var coverURL : String!
    
    var message : MBDMessageModel! {
        didSet {
            let titleAttributeString = NSMutableAttributedString(string: "服务通知")
            titleAttributeString.yy_font = UIFont.boldSystemFont(ofSize: 16)
            titleAttributeString.setDynamicTextColor()
            
            let titlecontainer = YYTextContainer()
            let titleWidth = CGRectGetWidth(UIScreen.main.bounds) - 15 - 60 - 15 - 15
            titlecontainer.size = CGSizeMake(titleWidth , CGFLOAT_MAX)
            titlecontainer.maximumNumberOfRows = 1
            
            self.titlelayout = YYTextLayout(container: titlecontainer, text: titleAttributeString)
            
            
            let subtitleAttributeString = NSMutableAttributedString(string: self.message.content ?? "")
            subtitleAttributeString.yy_font = UIFont.systemFont(ofSize: 12)
            subtitleAttributeString.yy_lineSpacing = 4
            subtitleAttributeString.setDynamicTextColor()
            
            let subtitleContainer = YYTextContainer()
            let subtitleWidth = CGRectGetWidth(UIScreen.main.bounds) - MBDPostCellModel.marginSubtitleLeft - MBDPostCellModel.marginSubtitleRight
            subtitleContainer.size = CGSizeMake(subtitleWidth , CGFLOAT_MAX)
            subtitleContainer.maximumNumberOfRows = 3
            
            self.subtitlelayout = YYTextLayout(container: subtitleContainer, text: subtitleAttributeString)
            
            self.coverURL = MBDPath2URLUtils.filePathPrefix.appending("ico_msg_notify")
        }
        
    }
    
    var cellHeight : CGFloat {
        return 66
    }
    
}

class MBDMessageViewModel : NSObject {
    
    
    var cellModels : Array<MBDMessageCellModel>?
    
    override init() {
        super.init()
        cellModels = MBDMessageViewModel.defaultCellModels()
        
    }
    
    static func defaultCellModels() -> Array<MBDMessageCellModel>{
        var cellModels = Array<MBDMessageCellModel>()
        
//        let descs = ["恭喜您发布的群【陕西人在上海】已通过审核"]
//        
//        for desc in descs {
//            let cellModel1 = MBDMessageCellModel()
//            
//            let msgModel = MBDMessageModel()
//            msgModel.messageId = "1234567"
//
//            cellModel1.message = msgModel
//            
//            cellModels.append(cellModel1)
//        }
        
        
        return cellModels
    }
    
    func doRequest(completion : @escaping (_ error : Error?, _ result : Array<MBDMessageCellModel>?,_ total : Int)->()) {
        
        MBDHttpClient.shared.doGetRequest("/userMessage/listPage").httpClientResponse { error, response in
            
            if nil != error {
                completion(error,nil,0)
            } else {
                let rawDict = response?.rawDict!
                let rawRows = rawDict?["data"]
                let total = rawDict?["total"] as! Int
                if rawRows is Array<Any> {
                    let rows = rawRows as! Array<Any>
                    var multiRows = Array<MBDMessageCellModel>()

                    for row in rows {
                        let item = MBDMessageModel(JSON(rawValue: row) ?? JSON(NSObject()))
                        let cellModel = MBDMessageCellModel()
                        cellModel.message = item
                        multiRows.append(cellModel)
                    }
                    completion(error,multiRows,total)
                } else {
                    completion(error,nil,0)
                }
                
            }
            
        }
    }
    
}
